package io.renren.modules.report.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import io.renren.modules.report.entity.BusinessPackageEntity;
import io.renren.modules.report.entity.PackageUserEntity;
import io.renren.modules.report.entity.UserPanelShareEntity;
import io.renren.modules.report.service.DataPackageService;
import io.renren.modules.report.service.UserPanelShareService;
import io.renren.modules.report.vo.SharedVO;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.report.entity.PanelPackageEntity;
import io.renren.modules.report.service.PanelPackageService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 
 *
 * @author august
 * @email 379249906@qq.com
 * @date 2020-07-15 17:54:56
 */
@RestController
@RequestMapping("report/panelpackage")
public class PanelPackageController {
    @Autowired
    private PanelPackageService panelPackageService;

    @Autowired
    private DataPackageService dataPackageService;

    @Autowired
    private UserPanelShareService userPanelShareService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = panelPackageService.queryPage(params);

        return R.ok().put("page", page);
    }

    @RequestMapping("/panels/{dataId}")
    public R panels(@PathVariable("dataId") Long dataId){
        List<PanelPackageEntity> panelUserEntities = panelPackageService.list(new QueryWrapper<PanelPackageEntity>().eq("dataId", dataId));

        return R.ok().put("panelList", panelUserEntities);
    }

    @RequestMapping("/panel/{userId}")
    public R panel(@PathVariable("userId") Long userId){
        UserPanelShareEntity entity = userPanelShareService.getOne(new QueryWrapper<UserPanelShareEntity>().eq("user_id", userId));
        PanelPackageEntity panel = panelPackageService.getById(entity.getPanelId());

        //List<PanelPackageEntity> panelUserEntities = panelPackageService.list(new QueryWrapper<PanelPackageEntity>().eq("dataId", dataId));

        return R.ok().put("panel", panel);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Long id){
        PanelPackageEntity panelPackage = panelPackageService.getById(id);

        return R.ok().put("panelPackage", panelPackage);
    }

    @PostMapping("/watch/{userId}")
    public R watch(@PathVariable("userId") Long userId){
        UserPanelShareEntity entity = userPanelShareService.getOne(new QueryWrapper<UserPanelShareEntity>().eq("user_id", userId));

       // panelPackageService

        return null;
    }

    @PostMapping("/share")
    public R share(@RequestBody SharedVO sharedVO){

      //  PanelPackageEntity panelPackage = panelPackageService.getById(id);

//        UserPanelShareEntity userPanelShareEntity = new UserPanelShareEntity();
//        BeanUtils.copyProperties(sharedVO,userPanelShareEntity);
        PanelPackageEntity panelPackageEntity = new PanelPackageEntity();
        BeanUtils.copyProperties(sharedVO,panelPackageEntity);
        panelPackageEntity.setId(sharedVO.getPanelId());

        //更新panel表
        panelPackageService.updateById(panelPackageEntity);

        userPanelShareService.remove(new QueryWrapper<UserPanelShareEntity>().eq("panel_id",sharedVO.getPanelId()));
        sharedVO.getUserIds().stream().forEach(item->{
            UserPanelShareEntity userPanelShareEntity = new UserPanelShareEntity();
            userPanelShareEntity.setPanelId(sharedVO.getPanelId());
            userPanelShareEntity.setUserId(item);
            userPanelShareService.save(userPanelShareEntity);
        });

        return R.ok();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody PanelPackageEntity panelPackage){
		panelPackageService.save(panelPackage);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody PanelPackageEntity panelPackage){
		panelPackageService.updateById(panelPackage);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] ids){
		panelPackageService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
